package com.rayson.oneapi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rayson.oneapi.annotation.AuthCheck;
import com.rayson.oneapi.common.R;
import com.rayson.oneapi.constant.UserConstant;
import com.rayson.oneapi.exception.BusinessException;
import com.rayson.oneapi.model.entity.User;
import com.rayson.oneapi.model.enums.ErrorCode;
import com.rayson.oneapi.model.req.user.*;
import com.rayson.oneapi.model.vo.LoginUserVO;
import com.rayson.oneapi.model.vo.UserVO;
import com.rayson.oneapi.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Author: rayson
 * Description:
 * Version: 1.0
 * Date: 2024-09-02 21:46
 */
@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;


    @ApiOperation("注册用户")
    @PostMapping("/register")
    public R<Void> userRegister(@RequestBody @Valid UserRegisterRequest userRegisterRequest) {
        userService.userRegister(userRegisterRequest);
        return R.ok(null, "注册成功");
    }


    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R<LoginUserVO> userLogin(@RequestBody @Valid UserLoginRequest userLoginRequest, HttpServletRequest request) {
        LoginUserVO loginUserVO = userService.userLogin(userLoginRequest, request);
        return R.ok(loginUserVO);
    }


    @ApiOperation("注销登录")
    @PostMapping("/logout")
    public R<Boolean> userLogout(HttpServletRequest request) {
        boolean result = userService.userLogout(request);
        return R.ok(result, "已注销登录");
    }


    @ApiOperation("获取当前已登录用户信息")
    @GetMapping("/cur")
    public R<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return R.ok(userService.getLoginUserVO(user));
    }


    @ApiOperation("新增用户")
    @PostMapping("/")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public R<Void> addUser(@RequestBody @Valid UserAddRequest userAddRequest) {
        userService.addUser(userAddRequest);
        return R.ok(null, "新增用户成功");
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public R<Boolean> deleteUser(@PathVariable String id) {
        log.info("删除用户，入参：{}", id);
        boolean flag = userService.removeById(id);
        return flag ? R.ok(null, "删除成功") : R.error("删除失败");
    }

    @ApiOperation("更新用户")
    @PutMapping("/")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public R<Void> updateUser(@RequestBody @Valid UserUpdateRequest userUpdateRequest) {
        userService.updateUser(userUpdateRequest);
        return R.ok(null, "更新成功");
    }

    @ApiOperation("根据id获取用户(仅管理员)")
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public R<User> getUserById(String id) {
        if (!StringUtils.hasText(id)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getById(id);
        return R.ok(user);
    }

    @ApiOperation("根据id获取用户封装(仅管理员)")
    @GetMapping("/get/vo")
    public R<UserVO> getUserVOById(String id) {
        R<User> response = getUserById(id);
        User user = response.getData();
        return R.ok(userService.getUserVO(user));
    }

    @ApiOperation("分页获取用户列表(仅管理员)")
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public R<Page<User>> listUserByPage(@RequestBody UserQueryRequest userQueryRequest) {
        long current = userQueryRequest.getPageNum();
        long size = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<>(current, size), userService.getQueryWrapper(userQueryRequest));
        return R.ok(userPage);
    }

    @ApiOperation("分页获取用户封装列表(仅管理员)")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/list/page/vo")
    public R<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryRequest userQueryRequest) {
        long current = userQueryRequest.getPageNum();
        long size = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<>(current, size), userService.getQueryWrapper(userQueryRequest));
        Page<UserVO> userVOPage = new Page<>(current, size, userPage.getTotal());
        List<UserVO> userVO = userService.getUserVO(userPage.getRecords());
        userVOPage.setRecords(userVO);
        return R.ok(userVOPage);
    }


    @ApiOperation("更新个人信息")
    @PutMapping("/mine")
    public R<Void> updateMyUser(@RequestBody UserUpdateMyRequest userUpdateMyRequest, HttpServletRequest request) {
        userService.updateUserBySelf(userUpdateMyRequest, request);
        return R.ok(null, "更新成功");
    }

}
